
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>8.5. 定义数据存储 · MobX 中文文档</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="react-performance.html" />
    
    
    <link rel="prev" href="trace.html" />
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"buttons":[{"user":"SangKa","repo":"MobX-Docs-CN","type":"star","size":"small","count":true},{"user":"SangKa","repo":"MobX-Docs-CN","type":"fork","size":"small","count":true}]};
    </script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    1. MobX 介绍
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="../intro/overview.html">
            
                <a href="../intro/overview.html">
            
                    
                    1.1. MobX 要点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2" data-path="../intro/concepts.html">
            
                <a href="../intro/concepts.html">
            
                    
                    1.2. 概念与原则
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../refguide/api.html">
            
                <a href="../refguide/api.html">
            
                    
                    2. API概览
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    3. observable 相关
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../refguide/observable.html">
            
                <a href="../refguide/observable.html">
            
                    
                    3.1. observable
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../refguide/observable-decorator.html">
            
                <a href="../refguide/observable-decorator.html">
            
                    
                    3.2. @observable
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../refguide/object.html">
            
                <a href="../refguide/object.html">
            
                    
                    3.3. objects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../refguide/array.html">
            
                <a href="../refguide/array.html">
            
                    
                    3.4. arrays
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../refguide/map.html">
            
                <a href="../refguide/map.html">
            
                    
                    3.5. maps
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../refguide/boxed.html">
            
                <a href="../refguide/boxed.html">
            
                    
                    3.6. boxed values
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../refguide/modifiers.html">
            
                <a href="../refguide/modifiers.html">
            
                    
                    3.7 decorators
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    4. 对 observables 作出响应
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../refguide/computed-decorator.html">
            
                <a href="../refguide/computed-decorator.html">
            
                    
                    4.1. (@)computed
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../refguide/autorun.html">
            
                <a href="../refguide/autorun.html">
            
                    
                    4.2. autorun
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../refguide/when.html">
            
                <a href="../refguide/when.html">
            
                    
                    4.3. when
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../refguide/reaction.html">
            
                <a href="../refguide/reaction.html">
            
                    
                    4.4. reaction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../refguide/observer-component.html">
            
                <a href="../refguide/observer-component.html">
            
                    
                    4.5. (@)observer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="react.html">
            
                <a href="react.html">
            
                    
                    4.6. 理解 MobX 对何作出响应
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    5. 改变 observables
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../refguide/action.html">
            
                <a href="../refguide/action.html">
            
                    
                    5.1. action
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="actions.html">
            
                <a href="actions.html">
            
                    
                    5.2. async actions & flows
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../refguide/object-api.html">
            
                <a href="../refguide/object-api.html">
            
                    
                    5.3. Object api
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    6. 工具函数
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../refguide/tojson.html">
            
                <a href="../refguide/tojson.html">
            
                    
                    6.1. toJS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../refguide/extend-observable.html">
            
                <a href="../refguide/extend-observable.html">
            
                    
                    6.2. extendObservable
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../refguide/extending.html">
            
                <a href="../refguide/extending.html">
            
                    
                    6.3. createAtom
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../refguide/observe.html">
            
                <a href="../refguide/observe.html">
            
                    
                    6.4. intercept & observe
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#frompromise">
            
                    
                    6.5. mobxUtils.fromPromise
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#fromresource">
            
                    
                    6.6. mobxUtils.fromResource
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#tostream">
            
                    
                    6.7. mobxUtils.toStream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#fromstream">
            
                    
                    6.8. mobxUtils.fromStream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#now">
            
                    
                    6.9. mobxUtils.now
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="../refguide/create-transformer.html">
            
                <a href="../refguide/create-transformer.html">
            
                    
                    6.10. mobxUtils.createTransformer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.11" data-path="../refguide/expr.html">
            
                <a href="../refguide/expr.html">
            
                    
                    6.11. mobxUtils.expr
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.12" data-path="../refguide/api.html">
            
                <a href="../refguide/api.html">
            
                    
                    6.12. complete api overview
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <a target="_blank" href="https://github.com/mobxjs/awesome-mobx#examples">
            
                    
                    7. 博客、 视频及相关项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" >
            
                <span>
            
                    
                    8. 贴士与技巧
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../faq/faq.html">
            
                <a href="../faq/faq.html">
            
                    
                    8.1. 常见问题解答
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="pitfalls.html">
            
                <a href="pitfalls.html">
            
                    
                    8.2. 常见陷阱与最佳实践
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="decorators.html">
            
                <a href="decorators.html">
            
                    
                    8.3. 如何(不)使用装饰器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="trace.html">
            
                <a href="trace.html">
            
                    
                    8.4. 使用 trace 进行调试
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.8.5" data-path="store.html">
            
                <a href="store.html">
            
                    
                    8.5. 定义数据存储
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.6" data-path="react-performance.html">
            
                <a href="react-performance.html">
            
                    
                    8.6. 优化 React 组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.7" data-path="devtools.html">
            
                <a href="devtools.html">
            
                    
                    8.7. 开发工具
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.8" data-path="../refguide/spy.html">
            
                <a href="../refguide/spy.html">
            
                    
                    8.8. spy
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../donating.html">
            
                <a href="../donating.html">
            
                    
                    9. 捐赠
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >8.5. 定义数据存储</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6784;&#x5EFA;&#x5927;&#x578B;&#x53EF;&#x6269;&#x5C55;&#x53EF;&#x7EF4;&#x62A4;&#x9879;&#x76EE;&#x7684;&#x6700;&#x4F73;&#x5B9E;&#x8DF5;"><a name="&#x6784;&#x5EFA;&#x5927;&#x578B;&#x53EF;&#x6269;&#x5C55;&#x53EF;&#x7EF4;&#x62A4;&#x9879;&#x76EE;&#x7684;&#x6700;&#x4F73;&#x5B9E;&#x8DF5;" class="plugin-anchor" href="#&#x6784;&#x5EFA;&#x5927;&#x578B;&#x53EF;&#x6269;&#x5C55;&#x53EF;&#x7EF4;&#x62A4;&#x9879;&#x76EE;&#x7684;&#x6700;&#x4F73;&#x5B9E;&#x8DF5;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6784;&#x5EFA;&#x5927;&#x578B;&#x53EF;&#x6269;&#x5C55;&#x53EF;&#x7EF4;&#x62A4;&#x9879;&#x76EE;&#x7684;&#x6700;&#x4F73;&#x5B9E;&#x8DF5;</h1>
<p>&#x672C;&#x7AE0;&#x8282;&#x4E3B;&#x8981;&#x662F;&#x5728; Mendix &#x4F7F;&#x7528; MobX &#x5DE5;&#x4F5C;&#x4E2D;&#x53D1;&#x73B0;&#x7684;&#x4E00;&#x4E9B;&#x6700;&#x4F73;&#x5B9E;&#x8DF5;&#x3002;
&#x672C;&#x7AE0;&#x8282;&#x5B8C;&#x5168;&#x662F;&#x51FA;&#x4E8E;&#x4E2A;&#x4EBA;&#x89C1;&#x89E3;&#x7684;&#xFF0C;&#x4F60;&#x5B8C;&#x5168;&#x4E0D;&#x5FC5;&#x5F3A;&#x884C;&#x5E94;&#x7528;&#x8FD9;&#x4E9B;&#x5B9E;&#x8DF5;&#x3002;
&#x6709;&#x5F88;&#x591A;&#x79CD;&#x4F7F;&#x7528; MobX &#x548C; React &#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x8FD9;&#x4EC5;&#x4EC5;&#x53EA;&#x662F;&#x5176;&#x4E2D;&#x7684;&#x4E00;&#x4E2A;&#x3002;</p>
<p>&#x672C;&#x7AE0;&#x4E3B;&#x8981;&#x4ECB;&#x7ECD;&#x4E00;&#x79CD;&#x4F7F;&#x7528; MobX &#x7684;&#x4E0D;&#x5510;&#x7A81;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x5B83;&#x5728;&#x73B0;&#x6709;&#x7684;&#x4EE3;&#x7801;&#x5E93;&#x4E2D;&#xFF0C;&#x6216;&#x8005;&#x642D;&#x914D;&#x7ECF;&#x5178;&#x7684; MVC &#x6A21;&#x5F0F;&#x8868;&#x73B0;&#x826F;&#x597D;&#x3002;&#x4F5C;&#x4E3A;&#x66FF;&#x4EE3;&#x65B9;&#x6848;&#xFF0C;&#x8FD8;&#x6709;&#x4E00;&#x79CD;&#x7EC4;&#x7EC7; stores &#x66F4;&#x4E13;&#x7528;&#x7684;&#x65B9;&#x5F0F;&#x662F;&#x4F7F;&#x7528; <a href="https://github.com/mobxjs/mobx-state-tree" target="_blank">mobx-state-tree</a>&#xFF0C;&#x5B83;&#x5177;&#x6709;&#x4E00;&#x4E9B;&#x5F88;&#x9177;&#x7684;&#x81EA;&#x5E26;&#x529F;&#x80FD;: &#x7ED3;&#x6784;&#x5171;&#x4EAB;&#x5FEB;&#x7167;&#x3001;&#x52A8;&#x4F5C;&#x4E2D;&#x95F4;&#x4EF6;&#x3001;JSON &#x8865;&#x4E01;&#x652F;&#x6301;&#xFF0C;&#x7B49;&#x7B49;&#x3002;</p>
<h1 id="stores&#x5B58;&#x50A8;"><a name="stores&#x5B58;&#x50A8;" class="plugin-anchor" href="#stores&#x5B58;&#x50A8;"><i class="fa fa-link" aria-hidden="true"></i></a>Stores(&#x5B58;&#x50A8;)</h1>
<p>Store &#x53EF;&#x4EE5;&#x5728;&#x4EFB;&#x4F55; Flux &#x7CFB;&#x67B6;&#x6784;&#x4E2D;&#x627E;&#x5230;&#xFF0C;&#x53EF;&#x4EE5;&#x4E0E; MVC &#x6A21;&#x5F0F;&#x4E2D;&#x7684;&#x63A7;&#x5236;&#x5668;&#x8FDB;&#x884C;&#x6BD4;&#x8F83;&#x3002;
Store &#x7684;&#x4E3B;&#x8981;&#x804C;&#x8D23;&#x662F;&#x5C06;<strong>&#x903B;&#x8F91;</strong>&#x548C;<strong>&#x72B6;&#x6001;</strong>&#x4ECE;&#x7EC4;&#x4EF6;&#x4E2D;&#x79FB;&#x81F3;&#x4E00;&#x4E2A;&#x72EC;&#x7ACB;&#x7684;&#xFF0C;&#x53EF;&#x6D4B;&#x8BD5;&#x7684;&#x5355;&#x5143;&#xFF0C;&#x8FD9;&#x4E2A;&#x5355;&#x5143;&#x5728; JavaScript &#x524D;&#x7AEF;&#x548C;&#x540E;&#x7AEF;&#x4E2D;&#x90FD;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x3002;</p>
<h2 id="&#x7528;&#x6237;&#x754C;&#x9762;&#x72B6;&#x6001;&#x7684;-store"><a name="&#x7528;&#x6237;&#x754C;&#x9762;&#x72B6;&#x6001;&#x7684;-store" class="plugin-anchor" href="#&#x7528;&#x6237;&#x754C;&#x9762;&#x72B6;&#x6001;&#x7684;-store"><i class="fa fa-link" aria-hidden="true"></i></a>&#x7528;&#x6237;&#x754C;&#x9762;&#x72B6;&#x6001;&#x7684; store</h2>
<p>&#x81F3;&#x5C11;&#x4E24;&#x4E2A; store &#x53EF;&#x4EE5;&#x8BA9;&#x7EDD;&#x5927;&#x591A;&#x6570;&#x5E94;&#x7528;&#x4ECE;&#x4E2D;&#x53D7;&#x76CA;&#x3002;
&#x4E00;&#x4E2A;&#x7528;&#x4E8E; <strong>UI &#x72B6;&#x6001;</strong>&#xFF0C;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x7528;&#x4E8E;<strong>&#x9886;&#x57DF;&#x72B6;&#x6001;</strong>&#x3002;
&#x5206;&#x79BB;&#x8FD9;&#x4E24;&#x4E2A; store &#x7684;&#x4F18;&#x70B9;&#x662F;&#x53EF;&#x4EE5;&#x91CD;&#x7528;&#x548C;&#x6D4B;&#x8BD5;<strong>&#x9886;&#x57DF;&#x72B6;&#x6001;</strong>&#xFF0C;&#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x5F88;&#x597D;&#x5730;&#x5728;&#x5176;&#x4ED6;&#x5E94;&#x7528;&#x4E2D;&#x91CD;&#x7528;&#x5B83;&#x3002;
&#x7136;&#x800C;&#xFF0C;<strong>UI &#x72B6;&#x6001; store</strong> &#x5BF9;&#x4E8E;&#x4F60;&#x7684;&#x5E94;&#x7528;&#x6765;&#x8BF4;&#x901A;&#x5E38;&#x975E;&#x5E38;&#x7279;&#x522B;&#x3002;
&#x4F46;&#x901A;&#x5E38;&#x4E5F;&#x5F88;&#x7B80;&#x5355;&#x3002;
&#x8FD9;&#x4E2A; store &#x901A;&#x5E38;&#x6CA1;&#x6709;&#x592A;&#x591A;&#x7684;&#x903B;&#x8F91;&#xFF0C;&#x4F46;&#x4F1A;&#x5B58;&#x50A8;&#x5927;&#x91CF;&#x7684;&#x677E;&#x6563;&#x8026;&#x5408;&#x7684; UI &#x76F8;&#x5173;&#x7684;&#x4FE1;&#x606F;&#x3002;
&#x8FD9;&#x662F;&#x7406;&#x60F3;&#x72B6;&#x51B5;&#x4E0B;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;&#x5927;&#x591A;&#x6570;&#x5E94;&#x7528;&#x5728;&#x5F00;&#x53D1;&#x8FC7;&#x7A0B;&#x4E2D;&#x4F1A;&#x7ECF;&#x5E38;&#x6027;&#x5730;&#x6539;&#x53D8; UI &#x72B6;&#x6001;&#x3002;</p>
<p>&#x901A;&#x5E38;&#x53EF;&#x4EE5;&#x5728; UI stores &#x4E2D;&#x627E;&#x5230;&#x7684;:</p>
<ul>
<li>Session &#x4FE1;&#x606F;</li>
<li>&#x5E94;&#x7528;&#x5DF2;&#x7ECF;&#x52A0;&#x8F7D;&#x4E86;&#x7684;&#x76F8;&#x5173;&#x4FE1;&#x606F;</li>
<li>&#x4E0D;&#x4F1A;&#x5B58;&#x50A8;&#x5230;&#x540E;&#x7AEF;&#x7684;&#x4FE1;&#x606F;</li>
<li>&#x5168;&#x5C40;&#x6027;&#x5F71;&#x54CD; UI &#x7684;&#x4FE1;&#x606F;<ul>
<li>&#x7A97;&#x53E3;&#x5C3A;&#x5BF8;</li>
<li>&#x53EF;&#x8BBF;&#x95EE;&#x6027;&#x4FE1;&#x606F;</li>
<li>&#x5F53;&#x524D;&#x8BED;&#x8A00;</li>
<li>&#x5F53;&#x524D;&#x6D3B;&#x52A8;&#x4E3B;&#x9898;</li>
</ul>
</li>
<li>&#x7528;&#x6237;&#x754C;&#x9762;&#x72B6;&#x6001;&#x77AC;&#x65F6;&#x5F71;&#x54CD;&#x591A;&#x4E2A;&#x3001;&#x6BEB;&#x4E0D;&#x76F8;&#x5173;&#x7684;&#x7EC4;&#x4EF6;:<ul>
<li>&#x5F53;&#x524D;&#x9009;&#x62E9;</li>
<li>&#x5DE5;&#x5177;&#x680F;&#x53EF;&#x89C1;&#x6027;, &#x7B49;&#x7B49;</li>
<li>&#x5411;&#x5BFC;&#x7684;&#x72B6;&#x6001;</li>
<li>&#x5168;&#x5C40;&#x53E0;&#x52A0;&#x7684;&#x72B6;&#x6001;</li>
</ul>
</li>
</ul>
<p>&#x8FD9;&#x4E9B;&#x4FE1;&#x606F;&#x5F00;&#x59CB;&#x4F5C;&#x4E3A;&#x67D0;&#x4E2A;&#x7279;&#x5B9A;&#x7EC4;&#x4EF6;&#x7684;&#x5185;&#x90E8;&#x72B6;&#x6001;&#x4F1A;&#x662F;&#x4E0D;&#x9519;&#x7684;&#x9009;&#x62E9;(&#x4F8B;&#x5982;&#x5DE5;&#x5177;&#x680F;&#x7684;&#x53EF;&#x89C1;&#x6027;)&#x3002;
&#x4F46;&#x8FC7;&#x4E86;&#x4E00;&#x6BB5;&#x65F6;&#x95F4;&#xFF0C;&#x4F60;&#x53D1;&#x73B0;&#x5E94;&#x7528;&#x4E2D;&#x7684;&#x5176;&#x4ED6;&#x5730;&#x65B9;&#x4E5F;&#x9700;&#x8981;&#x8FD9;&#x4E9B;&#x4FE1;&#x606F;&#x3002;
&#x60A8;&#x53EA;&#x9700;&#x5C06;&#x72B6;&#x6001;&#x79FB;&#x52A8;&#x5230; <strong>UI &#x72B6;&#x6001; store</strong>&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x5728;&#x7EC4;&#x4EF6;&#x6811;&#x4E2D;&#x5411;&#x4E0A;&#x63A8;&#x52A8;&#x72B6;&#x6001;&#xFF0C;&#x5C31;&#x50CF;&#x5728;&#x666E;&#x901A;&#x7684; React &#x5E94;&#x7528;&#x4E2D;&#x6240;&#x505A;&#x7684;&#x90A3;&#x6837;&#x3002;</p>
<p>&#x5BF9;&#x4E8E;&#x540C;&#x6784;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#xFF0C;&#x4F60;&#x53EF;&#x80FD;&#x8FD8;&#x5E0C;&#x671B;&#x4F7F;&#x7528;&#x6B63;&#x5E38;&#x9ED8;&#x8BA4;&#x503C;&#x63D0;&#x4F9B;&#x8FD9;&#x4E2A; store &#x7684;&#x5B58;&#x6839;&#x5B9E;&#x73B0;&#xFF0C;&#x4EE5;&#x4FBF;&#x6240;&#x6709;&#x7EC4;&#x4EF6;&#x6309;&#x9884;&#x671F;&#x5448;&#x73B0;&#x3002;
&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5728;&#x5E94;&#x7528;&#x4E2D;&#x4F20;&#x9012;&#x5C5E;&#x6027;&#x5230;&#x7EC4;&#x4EF6;&#x6811;&#x4E2D;&#x6216;&#x4F7F;&#x7528; <code>mobx-react</code> &#x5305;&#x4E2D;&#x7684; <code>Provider</code> &#x548C; <code>inject</code> &#x6765;&#x5206;&#x53D1; <strong>UI &#x72B6;&#x6001; store</strong>&#x3002;</p>
<p>store &#x793A;&#x4F8B; (&#x4F7F;&#x7528; ES6 &#x8BED;&#x6CD5;):</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> {observable, computed, asStructure} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;mobx&apos;</span>;
<span class="hljs-keyword">import</span> jquery <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;jquery&apos;</span>;

<span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UiState</span> </span>{
    @observable language = <span class="hljs-string">&quot;en_US&quot;</span>;
    @observable pendingRequestCount = <span class="hljs-number">0</span>;

    <span class="hljs-comment">// .struct &#x786E;&#x4FDD;&#x4E0D;&#x4F1A;&#x901A;&#x77E5;&#x89C2;&#x5BDF;&#x8005;&#xFF0C;&#x9664;&#x975E;&#x5C3A;&#x5BF8;&#x5BF9;&#x8C61;&#x4EE5;&#x6DF1;&#x5EA6;&#x76F8;&#x7B49;&#x7684;&#x65B9;&#x5F0F;&#x6539;&#x53D8;</span>
    @observable.struct windowDimensions = {
        width: jquery(<span class="hljs-built_in">window</span>).width(),
        height: jquery(<span class="hljs-built_in">window</span>).height()
    };

    <span class="hljs-keyword">constructor</span>() {
        jquery.resize(() =&gt; {
            <span class="hljs-keyword">this</span>.windowDimensions = getWindowDimensions();
        });
    }

    @computed get appIsInSync() {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.pendingRequestCount === <span class="hljs-number">0</span>
    }
}
</code></pre>
<h2 id="&#x9886;&#x57DF;-store"><a name="&#x9886;&#x57DF;-store" class="plugin-anchor" href="#&#x9886;&#x57DF;-store"><i class="fa fa-link" aria-hidden="true"></i></a>&#x9886;&#x57DF; store</h2>
<p>&#x4F60;&#x7684;&#x5E94;&#x7528;&#x5E94;&#x8BE5;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;<strong>&#x9886;&#x57DF;</strong> store&#x3002;
&#x8FD9;&#x4E9B; store &#x5B58;&#x50A8;&#x4F60;&#x7684;&#x5E94;&#x7528;&#x6240;&#x5173;&#x5FC3;&#x7684;&#x6570;&#x636E;&#x3002;
&#x5F85;&#x529E;&#x4E8B;&#x9879;&#x3001;&#x7528;&#x6237;&#x3001;&#x4E66;&#x3001;&#x7535;&#x5F71;&#x3001;&#x8BA2;&#x5355;&#x3001;&#x51E1;&#x662F;&#x4F60;&#x80FD;&#x8BF4;&#x51FA;&#x7684;&#x3002;
&#x4F60;&#x7684;&#x5E94;&#x7528;&#x5F88;&#x6709;&#x53EF;&#x80FD;&#x81F3;&#x5C11;&#x6709;&#x4E00;&#x4E2A;&#x9886;&#x57DF; store&#x3002;</p>
<p>&#x5355;&#x4E2A;&#x9886;&#x57DF; store &#x5E94;&#x8BE5;&#x8D1F;&#x8D23;&#x5E94;&#x7528;&#x4E2D;&#x7684;&#x5355;&#x4E2A;&#x6982;&#x5FF5;&#x3002;
&#x7136;&#x800C;&#xFF0C;&#x5355;&#x4E2A;&#x6982;&#x5FF5;&#x53EF;&#x4EE5;&#x91C7;&#x53D6;&#x591A;&#x4E2A;&#x5B50;&#x7C7B;&#x578B;&#x7684;&#x5F62;&#x5F0F;&#xFF0C;&#x5E76;&#x4E14;&#x5B83;&#x901A;&#x5E38;&#x662F;(&#x5FAA;&#x73AF;)&#x6811;&#x7ED3;&#x6784;&#x3002;
&#x4E3E;&#x4F8B;&#x6765;&#x8BF4;&#xFF0C;&#x4E00;&#x4E2A;&#x9886;&#x57DF; store &#x8D1F;&#x8D23;&#x4EA7;&#x54C1;&#xFF0C;&#x4E00;&#x4E2A;&#x8D1F;&#x8D23;&#x8BA2;&#x5355;&#x3002;
&#x6839;&#x636E;&#x7ECF;&#x9A8C;&#x6765;&#x8BF4;&#xFF0C;&#x5982;&#x679C;&#x4E24;&#x4E2A;&#x6982;&#x5FF5;&#x4E4B;&#x95F4;&#x7684;&#x5173;&#x7CFB;&#x7684;&#x672C;&#x8D28;&#x662F;&#x5305;&#x542B;&#x7684;&#xFF0C;&#x5219;&#x5B83;&#x4EEC;&#x901A;&#x5E38;&#x5E94;&#x5728;&#x540C;&#x4E00;&#x4E2A; store &#x4E2D;&#x3002;
&#x6240;&#x4EE5;&#x8BF4;&#x4E00;&#x4E2A; store &#x53EA;&#x662F;&#x7BA1;&#x7406; <strong>&#x9886;&#x57DF;&#x5BF9;&#x8C61;</strong>&#x3002;</p>
<p>Store &#x7684;&#x804C;&#x8D23;:</p>
<ul>
<li>&#x5B9E;&#x4F8B;&#x5316;&#x9886;&#x57DF;&#x5BF9;&#x8C61;&#xFF0C; &#x786E;&#x4FDD;&#x9886;&#x57DF;&#x5BF9;&#x8C61;&#x77E5;&#x9053;&#x5B83;&#x4EEC;&#x6240;&#x5C5E;&#x7684; store&#x3002;</li>
<li>&#x786E;&#x4FDD;&#x6BCF;&#x4E2A;&#x9886;&#x57DF;&#x5BF9;&#x8C61;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x3002;
&#x540C;&#x4E00;&#x4E2A;&#x7528;&#x6237;&#x3001;&#x8BA2;&#x5355;&#x6216;&#x8005;&#x5F85;&#x529E;&#x4E8B;&#x9879;&#x4E0D;&#x5E94;&#x8BE5;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x5B58;&#x50A8;&#x4E24;&#x6B21;&#x3002;
&#x8FD9;&#x6837;&#xFF0C;&#x53EF;&#x4EE5;&#x5B89;&#x5168;&#x5730;&#x4F7F;&#x7528;&#x5F15;&#x7528;&#xFF0C;&#x5E76;&#x786E;&#x4FDD;&#x6B63;&#x5728;&#x67E5;&#x770B;&#x7684;&#x5B9E;&#x4F8B;&#x662F;&#x6700;&#x65B0;&#x7684;&#xFF0C;&#x800C;&#x65E0;&#x9700;&#x89E3;&#x6790;&#x5F15;&#x7528;&#x3002;
&#x5F53;&#x8C03;&#x8BD5;&#x65F6;&#x8FD9;&#x5341;&#x5206;&#x5FEB;&#x901F;&#x3001;&#x7B80;&#x5355;&#x3001;&#x65B9;&#x4FBF;&#x3002;</li>
<li>&#x63D0;&#x4F9B;&#x540E;&#x7AEF;&#x96C6;&#x6210;&#xFF0C;&#x5F53;&#x9700;&#x8981;&#x65F6;&#x5B58;&#x50A8;&#x6570;&#x636E;&#x3002;</li>
<li>&#x5982;&#x679C;&#x4ECE;&#x540E;&#x7AEF;&#x63A5;&#x6536;&#x5230;&#x66F4;&#x65B0;&#xFF0C;&#x5219;&#x66F4;&#x65B0;&#x73B0;&#x6709;&#x5B9E;&#x4F8B;&#x3002;</li>
<li>&#x4E3A;&#x4F60;&#x7684;&#x5E94;&#x7528;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x72EC;&#x7ACB;&#x3001;&#x901A;&#x7528;&#x3001;&#x53EF;&#x6D4B;&#x8BD5;&#x7684;&#x7EC4;&#x4EF6;&#x3002;</li>
<li>&#x8981;&#x786E;&#x4FDD; store &#x662F;&#x53EF;&#x6D4B;&#x8BD5;&#x7684;&#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x5728;&#x670D;&#x52A1;&#x7AEF;&#x8FD0;&#x884C;&#xFF0C;&#x4F60;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x5C06;&#x5B9E;&#x9645;&#x7684; websocket/http &#x8BF7;&#x6C42;&#x79FB;&#x5230;&#x5355;&#x72EC;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x4EE5;&#x4FBF;&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x901A;&#x4FE1;&#x5C42;&#x62BD;&#x8C61;&#x3002;</li>
<li>Store &#x5E94;&#x8BE5;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x3002;</li>
</ul>
<h3 id="&#x9886;&#x57DF;&#x5BF9;&#x8C61;"><a name="&#x9886;&#x57DF;&#x5BF9;&#x8C61;" class="plugin-anchor" href="#&#x9886;&#x57DF;&#x5BF9;&#x8C61;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x9886;&#x57DF;&#x5BF9;&#x8C61;</h3>
<p>&#x6BCF;&#x4E2A;&#x9886;&#x57DF;&#x5BF9;&#x8C61;&#x5E94;&#x4F7F;&#x7528;&#x81EA;&#x5DF1;&#x7684;&#x7C7B;(&#x6216;&#x6784;&#x9020;&#x51FD;&#x6570;)&#x6765;&#x8868;&#x793A;&#x3002;
&#x5EFA;&#x8BAE;&#x4EE5;<strong>&#x975E;&#x89C4;&#x8303;&#x5316;</strong>&#x5F62;&#x5F0F;&#x5B58;&#x50A8;&#x6570;&#x636E;&#x3002;
&#x4E0D;&#x5FC5;&#x628A;&#x5BA2;&#x6237;&#x7AEF;&#x5E94;&#x7528;&#x7684;&#x72B6;&#x6001;&#x770B;&#x505A;&#x6570;&#x636E;&#x5E93;&#x7684;&#x4E00;&#x79CD;&#x3002;
&#x771F;&#x5B9E;&#x5F15;&#x7528;&#x3001;&#x5FAA;&#x73AF;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x548C;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x90FD;&#x662F; JavaScript &#x4E2D;&#x975E;&#x5E38;&#x5F3A;&#x5927;&#x7684;&#x6982;&#x5FF5;&#x3002;
&#x5141;&#x8BB8;&#x9886;&#x57DF;&#x5BF9;&#x8C61;&#x76F4;&#x63A5;&#x5F15;&#x7528;&#x6765;&#x81EA;&#x5176;&#x4ED6; store &#x7684;&#x9886;&#x57DF;&#x5BF9;&#x8C61;&#x3002;
&#x8BB0;&#x4F4F;: &#x6211;&#x4EEC;&#x60F3;&#x4FDD;&#x6301;&#x6211;&#x4EEC;&#x7684;&#x64CD;&#x4F5C;&#x548C;&#x89C6;&#x56FE;&#x5C3D;&#x53EF;&#x80FD;&#x7B80;&#x5355;&#xFF0C;&#x5E76;&#x4E14;&#x9700;&#x8981;&#x7BA1;&#x7406;&#x5F15;&#x7528;&#x548C;&#x81EA;&#x5DF1;&#x505A;&#x5783;&#x573E;&#x56DE;&#x6536;&#x53EF;&#x80FD;&#x662F;&#x4E00;&#x79CD;&#x5012;&#x9000;&#x3002;
&#x4E0D;&#x540C;&#x4E8E;&#x5176;&#x4ED6; Flux &#x7CFB;&#x7EDF;&#x67B6;&#x6784;&#xFF0C;&#x4F7F;&#x7528; MobX &#x4E0D;&#x9700;&#x8981;&#x5BF9;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x6807;&#x51C6;&#x5316;&#xFF0C;&#x800C;&#x4E14;&#x8FD9;&#x4F7F;&#x5F97;&#x6784;&#x5EFA;&#x5E94;&#x7528;<strong>&#x672C;&#x8D28;&#x4E0A;</strong>&#x590D;&#x6742;&#x7684;&#x90E8;&#x5206;&#x53D8;&#x5F97;&#x66F4;&#x7B80;&#x5355;:
&#x4F60;&#x7684;&#x4E1A;&#x52A1;&#x89C4;&#x5219;&#x3001;&#x64CD;&#x4F5C;&#x548C;&#x7528;&#x6237;&#x754C;&#x9762;&#x3002;</p>
<p>&#x9886;&#x57DF;&#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x5C06;&#x5176;&#x6240;&#x6709;&#x903B;&#x8F91;&#x59D4;&#x6258;&#x7ED9;&#x5B83;&#x4EEC;&#x6240;&#x5C5E;&#x7684; store&#xFF0C;&#x5982;&#x679C;&#x8FD9;&#x66F4;&#x7B26;&#x5408;&#x4F60;&#x7684;&#x5E94;&#x7528;&#x7684;&#x8BDD;&#x3002;
&#x53EF;&#x4EE5;&#x5C06;&#x9886;&#x57DF;&#x5BF9;&#x8C61;&#x8868;&#x793A;&#x6210;&#x666E;&#x901A;&#x5BF9;&#x8C61;&#xFF0C;&#x4F46;&#x7C7B;&#x6BD4;&#x666E;&#x901A;&#x5BF9;&#x8C61;&#x6709;&#x4E00;&#x4E9B;&#x91CD;&#x8981;&#x7684;&#x4F18;&#x52BF;:</p>
<ul>
<li>&#x5B83;&#x4EEC;&#x53EF;&#x4EE5;&#x6709;&#x65B9;&#x6CD5;&#x3002;
&#x8FD9;&#x4F7F;&#x5F97;&#x9886;&#x57DF;&#x6982;&#x5FF5;&#x66F4;&#x5BB9;&#x6613;&#x72EC;&#x7ACB;&#x4F7F;&#x7528;&#xFF0C;&#x5E76;&#x51CF;&#x5C11;&#x5E94;&#x7528;&#x6240;&#x9700;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x611F;&#x77E5;&#x7684;&#x6570;&#x91CF;&#x3002;
&#x53EA;&#x662F;&#x4F20;&#x9012;&#x5BF9;&#x8C61;&#x3002;
&#x4F60;&#x4E0D;&#x9700;&#x8981;&#x4F20;&#x9012; store&#xFF0C;&#x6216;&#x8005;&#x5FC5;&#x987B;&#x5F04;&#x6E05;&#x695A;&#x54EA;&#x4E9B;&#x64CD;&#x4F5C;&#x53EF;&#x4EE5;&#x5728;&#x5BF9;&#x8C61;&#x4E0A;&#x5E94;&#x7528;&#xFF0C;&#x5982;&#x679C;&#x5B83;&#x4EEC;&#x53EA;&#x662F;&#x4F5C;&#x4E3A;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x53EF;&#x7528;&#x3002;</li>
<li>&#x5BF9;&#x4E8E;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#x7684;&#x53EF;&#x89C1;&#x6027;&#xFF0C;&#x5B83;&#x4EEC;&#x63D0;&#x4F9B;&#x4E86;&#x7EC6;&#x7C92;&#x5EA6;&#x7684;&#x63A7;&#x5236;&#x3002;</li>
<li>&#x4F7F;&#x7528;&#x6784;&#x9020;&#x51FD;&#x6570;&#x521B;&#x5EFA;&#x7684;&#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x81EA;&#x7531;&#x5730;&#x6DF7;&#x5408; observable &#x5C5E;&#x6027;&#x548C;&#x51FD;&#x6570;&#xFF0C;&#x4EE5;&#x53CA;&#x975E; observable &#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#x3002;</li>
<li>&#x5B83;&#x4EEC;&#x6613;&#x4E8E;&#x8BC6;&#x522B;&#xFF0C;&#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x8FDB;&#x884C;&#x4E25;&#x683C;&#x7684;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x3002;</li>
</ul>
<h3 id="&#x9886;&#x57DF;-store-&#x793A;&#x4F8B;"><a name="&#x9886;&#x57DF;-store-&#x793A;&#x4F8B;" class="plugin-anchor" href="#&#x9886;&#x57DF;-store-&#x793A;&#x4F8B;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x9886;&#x57DF; store &#x793A;&#x4F8B;</h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> {observable, autorun} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;mobx&apos;</span>;
<span class="hljs-keyword">import</span> uuid <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;node-uuid&apos;</span>;

<span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TodoStore</span> </span>{
    authorStore;
    transportLayer;
    @observable todos = [];
    @observable isLoading = <span class="hljs-literal">true</span>;

    <span class="hljs-keyword">constructor</span>(transportLayer, authorStore) {
        <span class="hljs-keyword">this</span>.authorStore = authorStore; <span class="hljs-comment">// &#x53EF;&#x4EE5;&#x4E3A;&#x6211;&#x4EEC;&#x63D0;&#x4F9B; author &#x7684; store</span>
        <span class="hljs-keyword">this</span>.transportLayer = transportLayer; <span class="hljs-comment">// &#x53EF;&#x4EE5;&#x4E3A;&#x6211;&#x4EEC;&#x53D1;&#x8D77;&#x670D;&#x52A1;&#x7AEF;&#x8BF7;&#x6C42;&#x7684;&#x4E1C;&#x897F;</span>
        <span class="hljs-keyword">this</span>.transportLayer.onReceiveTodoUpdate(updatedTodo =&gt; <span class="hljs-keyword">this</span>.updateTodoFromServer(updatedTodo));
        <span class="hljs-keyword">this</span>.loadTodos();
    }

    <span class="hljs-comment">/**
     * &#x4ECE;&#x670D;&#x52A1;&#x7AEF;&#x62C9;&#x53D6;&#x6240;&#x6709;&#x7684; todo
     */</span>
    loadTodos() {
        <span class="hljs-keyword">this</span>.isLoading = <span class="hljs-literal">true</span>;
        <span class="hljs-keyword">this</span>.transportLayer.fetchTodos().then(fetchedTodos =&gt; {
            fetchedTodos.forEach(json =&gt; <span class="hljs-keyword">this</span>.updateTodoFromServer(json));
            <span class="hljs-keyword">this</span>.isLoading = <span class="hljs-literal">false</span>;
        });
    }

    <span class="hljs-comment">/**
     * &#x4F7F;&#x7528;&#x670D;&#x52A1;&#x5668;&#x4E2D;&#x7684;&#x4FE1;&#x606F;&#x66F4;&#x65B0; todo&#x3002;&#x4FDD;&#x8BC1;&#x4E00;&#x4E2A; todo &#x53EA;&#x5B58;&#x5728;&#x4E00;&#x6B21;&#x3002;
     * &#x53EF;&#x80FD;&#x6784;&#x9020;&#x4E00;&#x4E2A;&#x65B0;&#x7684; todo&#xFF0C;&#x66F4;&#x65B0;&#x73B0;&#x6709;&#x7684; todo,
     * &#x6216;&#x5220;&#x9664; todo&#xFF0C;&#x5982;&#x679C;&#x5B83;&#x5DF2;&#x7ECF;&#x5728;&#x670D;&#x52A1;&#x5668;&#x4E0A;&#x88AB;&#x5220;&#x9664;&#x7684;&#x8BDD;&#x3002;
     */</span>
    updateTodoFromServer(json) {
        <span class="hljs-keyword">var</span> todo = <span class="hljs-keyword">this</span>.todos.find(todo =&gt; todo.id === json.id);
        <span class="hljs-keyword">if</span> (!todo) {
            todo = <span class="hljs-keyword">new</span> Todo(<span class="hljs-keyword">this</span>, json.id);
            <span class="hljs-keyword">this</span>.todos.push(todo);
        }
        <span class="hljs-keyword">if</span> (json.isDeleted) {
            <span class="hljs-keyword">this</span>.removeTodo(todo);
        } <span class="hljs-keyword">else</span> {
            todo.updateFromJson(json);
        }
    }

    <span class="hljs-comment">/**
     * &#x5728;&#x5BA2;&#x6237;&#x7AEF;&#x548C;&#x670D;&#x52A1;&#x7AEF;&#x90FD;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684; todo
     */</span>
    createTodo() {
        <span class="hljs-keyword">var</span> todo = <span class="hljs-keyword">new</span> Todo(<span class="hljs-keyword">this</span>);
        <span class="hljs-keyword">this</span>.todos.push(todo);
        <span class="hljs-keyword">return</span> todo;
    }

    <span class="hljs-comment">/**
     * &#x5982;&#x679C;&#x4E00;&#x4E2A; todo &#x88AB;&#x5220;&#x9664;&#x4E86;&#xFF0C;&#x5C06;&#x5176;&#x4ECE;&#x5BA2;&#x6237;&#x7AEF;&#x5185;&#x5B58;&#x4E2D;&#x6E05;&#x7406;&#x6389;
     */</span>
    removeTodo(todo) {
        <span class="hljs-keyword">this</span>.todos.splice(<span class="hljs-keyword">this</span>.todos.indexOf(todo), <span class="hljs-number">1</span>);
        todo.dispose();
    }
}

<span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Todo</span> </span>{

    <span class="hljs-comment">/**
     * todo &#x7684;&#x552F;&#x4E00; id, &#x4E0D;&#x53EF;&#x6539;&#x53D8;&#x3002;
     */</span>
    id = <span class="hljs-literal">null</span>;

    @observable completed = <span class="hljs-literal">false</span>;
    @observable task = <span class="hljs-string">&quot;&quot;</span>;

    <span class="hljs-comment">/**
     * &#x5F15;&#x7528;&#x4E00;&#x4E2A; author &#x5BF9;&#x8C61;(&#x6765;&#x81EA; authorStore)
     */</span>
    @observable author = <span class="hljs-literal">null</span>;

    store = <span class="hljs-literal">null</span>;

    <span class="hljs-comment">/**
     * &#x6307;&#x793A;&#x6B64;&#x5BF9;&#x8C61;&#x7684;&#x66F4;&#x6539;&#x662F;&#x5426;&#x5E94;&#x63D0;&#x4EA4;&#x5230;&#x670D;&#x52A1;&#x5668;
     */</span>
    autoSave = <span class="hljs-literal">true</span>;

    <span class="hljs-comment">/**
     * &#x4E3A;&#x81EA;&#x52A8;&#x5B58;&#x50A8;&#x6B64; Todo &#x7684;&#x526F;&#x4F5C;&#x7528;&#x63D0;&#x4F9B;&#x7684;&#x6E05;&#x7406;&#x65B9;&#x6CD5;
     * &#x53C2;&#x89C1; @dispose.
     */</span>
    saveHandler = <span class="hljs-literal">null</span>;

    <span class="hljs-keyword">constructor</span>(store, id=uuid.v4()) {
        <span class="hljs-keyword">this</span>.store = store;
        <span class="hljs-keyword">this</span>.id = id;

        <span class="hljs-keyword">this</span>.saveHandler = reaction(
            <span class="hljs-comment">// &#x89C2;&#x5BDF;&#x5728; JSON &#x4E2D;&#x4F7F;&#x7528;&#x4E86;&#x7684;&#x4EFB;&#x4F55;&#x4E1C;&#x897F;:</span>
            () =&gt; <span class="hljs-keyword">this</span>.asJson,
            <span class="hljs-comment">// &#x5982;&#x4F55; autoSave &#x4E3A; true, &#x628A; json &#x53D1;&#x9001;&#x5230;&#x670D;&#x52A1;&#x7AEF;</span>
            (json) =&gt; {
                <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.autoSave) {
                    <span class="hljs-keyword">this</span>.store.transportLayer.saveTodo(json);
                }
            }
        );
    }

    <span class="hljs-comment">/**
     * &#x5728;&#x5BA2;&#x6237;&#x7AEF;&#x548C;&#x670D;&#x52A1;&#x7AEF;&#x4E2D;&#x5220;&#x9664;&#x6B64; todo
     */</span>
    <span class="hljs-keyword">delete</span>() {
        <span class="hljs-keyword">this</span>.store.transportLayer.deleteTodo(<span class="hljs-keyword">this</span>.id);
        <span class="hljs-keyword">this</span>.store.removeTodo(<span class="hljs-keyword">this</span>);
    }

    @computed get asJson() {
        <span class="hljs-keyword">return</span> {
            id: <span class="hljs-keyword">this</span>.id,
            completed: <span class="hljs-keyword">this</span>.completed,
            task: <span class="hljs-keyword">this</span>.task,
            authorId: <span class="hljs-keyword">this</span>.author ? <span class="hljs-keyword">this</span>.author.id : <span class="hljs-literal">null</span>
        };
    }

    <span class="hljs-comment">/**
     * &#x4F7F;&#x7528;&#x670D;&#x52A1;&#x7AEF;&#x4FE1;&#x606F;&#x66F4;&#x65B0;&#x6B64; todo
     */</span>
    updateFromJson(json) {
        <span class="hljs-comment">// &#x8BF7;&#x786E;&#x4FDD;&#x6211;&#x4EEC;&#x7684;&#x66F4;&#x6539;&#x4E0D;&#x4F1A;&#x53D1;&#x9001;&#x56DE;&#x670D;&#x52A1;&#x5668;</span>
        <span class="hljs-keyword">this</span>.autoSave = <span class="hljs-literal">false</span>;
        <span class="hljs-keyword">this</span>.completed = json.completed;
        <span class="hljs-keyword">this</span>.task = json.task;
        <span class="hljs-keyword">this</span>.author = <span class="hljs-keyword">this</span>.store.authorStore.resolveAuthor(json.authorId);
        <span class="hljs-keyword">this</span>.autoSave = <span class="hljs-literal">true</span>;
    }

    dispose() {
        <span class="hljs-comment">// &#x6E05;&#x7406;&#x89C2;&#x5BDF;&#x8005;</span>
        <span class="hljs-keyword">this</span>.saveHandler();
    }
}
</code></pre>
<h1 id="&#x7EC4;&#x5408;&#x591A;&#x4E2A;-stores"><a name="&#x7EC4;&#x5408;&#x591A;&#x4E2A;-stores" class="plugin-anchor" href="#&#x7EC4;&#x5408;&#x591A;&#x4E2A;-stores"><i class="fa fa-link" aria-hidden="true"></i></a>&#x7EC4;&#x5408;&#x591A;&#x4E2A; stores</h1>
<p>&#x4E00;&#x4E2A;&#x7ECF;&#x5E38;&#x88AB;&#x95EE;&#x5230;&#x7684;&#x95EE;&#x9898;&#x5C31;&#x662F;&#xFF0C;&#x5982;&#x4F55;&#x4E0D;&#x4F7F;&#x7528;&#x5355;&#x4F8B;&#x6765;&#x7EC4;&#x5408;&#x591A;&#x4E2A; stores &#x3002;&#x5B83;&#x4EEC;&#x4E4B;&#x95F4;&#x5982;&#x4F55;&#x901A;&#x4FE1;&#x5462;&#xFF1F;</p>
<p>&#x4E00;&#x79CD;&#x9AD8;&#x6548;&#x7684;&#x6A21;&#x5F0F;&#x662F;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; <code>RootStore</code> &#x6765;&#x5B9E;&#x4F8B;&#x5316;&#x6240;&#x6709; stores &#xFF0C;&#x5E76;&#x5171;&#x4EAB;&#x5F15;&#x7528;&#x3002;&#x8FD9;&#x79CD;&#x6A21;&#x5F0F;&#x7684;&#x4F18;&#x52BF;&#x662F;:</p>
<ol>
<li>&#x8BBE;&#x7F6E;&#x7B80;&#x5355;</li>
<li>&#x5F88;&#x597D;&#x7684;&#x652F;&#x6301;&#x5F3A;&#x7C7B;&#x578B;</li>
<li>&#x4F7F;&#x5F97;&#x590D;&#x6742;&#x7684;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x53D8;&#x5F97;&#x7B80;&#x5355;&#xFF0C;&#x56E0;&#x4E3A;&#x4F60;&#x53EA;&#x9700;&#x8981;&#x5B9E;&#x4F8B;&#x5316;&#x4E00;&#x4E2A;&#x6839; store</li>
</ol>
<p>&#x793A;&#x4F8B;:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RootStore</span> </span>{
  <span class="hljs-keyword">constructor</span>() {
    <span class="hljs-keyword">this</span>.userStore = <span class="hljs-keyword">new</span> UserStore(<span class="hljs-keyword">this</span>)
    <span class="hljs-keyword">this</span>.todoStore = <span class="hljs-keyword">new</span> TodoStore(<span class="hljs-keyword">this</span>)
  }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserStore</span> </span>{
  <span class="hljs-keyword">constructor</span>(rootStore) {
    <span class="hljs-keyword">this</span>.rootStore = rootStore
  }

  getTodos(user) {
    <span class="hljs-comment">// &#x901A;&#x8FC7;&#x6839; store &#x6765;&#x8BBF;&#x95EE; todoStore</span>
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.rootStore.todoStore.todos.filter(todo =&gt; todo.author === user)
  }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TodoStore</span> </span>{
  @observable todos = []

  <span class="hljs-keyword">constructor</span>(rootStore) {
    <span class="hljs-keyword">this</span>.rootStore = rootStore
  }
}
</code></pre>
<p>&#x5F53;&#x4F7F;&#x7528; React &#x65F6;&#xFF0C;&#x8FD9;&#x4E2A;&#x6839; store &#x901A;&#x5E38;&#x4F1A;&#x901A;&#x8FC7;&#x4F7F;&#x7528; <code>&lt;Provider rootStore={new RootStore()}&gt;&lt;App /&gt;&lt;/Provider&gt;</code> &#x6765;&#x63D2;&#x5165;&#x5230;&#x7EC4;&#x4EF6;&#x6811;&#x4E4B;&#x4E2D;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="trace.html" class="navigation navigation-prev " aria-label="Previous page: 8.4. 使用 trace 进行调试">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="react-performance.html" class="navigation navigation-next " aria-label="Next page: 8.6. 优化 React 组件">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"8.5. 定义数据存储","level":"1.8.5","depth":2,"next":{"title":"8.6. 优化 React 组件","level":"1.8.6","depth":2,"path":"best/react-performance.md","ref":"best/react-performance.md","articles":[]},"previous":{"title":"8.4. 使用 trace 进行调试","level":"1.8.4","depth":2,"path":"best/trace.md","ref":"best/trace.md","articles":[]},"dir":"ltr"},"config":{"plugins":["github","edit-link","anchors","copy-code-button","github-buttons"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"github":{"url":"https://github.com/SangKa/mobx-docs-cn"},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"github-buttons":{"buttons":[{"user":"SangKa","repo":"MobX-Docs-CN","type":"star","size":"small","count":true},{"user":"SangKa","repo":"MobX-Docs-CN","type":"fork","size":"small","count":true}]},"copy-code-button":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"edit-link":{"label":"完善文档","base":"https://github.com/SangKa/mobx-docs-cn/tree/master/docs"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"MobX 中文文档","gitbook":"3.2.2"},"file":{"path":"best/store.md","mtime":"2018-09-18T13:06:39.940Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2018-09-18T13:06:52.064Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

